﻿Ext.namespace("Swfu");
Ext.namespace("Swfu.BackMgr");

Swfu.BackMgr.DocNoMgr = Ext.extend(Ext.grid.EditorGridPanel, {
    initComponent: function () {
        var Plant = Ext.data.Record.create([{ name: 'DocNo1' }, { name: 'DocName' }, { name: 'OrderNo'}]);
        //var sm = new Ext.grid.CheckboxSelectionModel({ singleSelect: true });
        var fm = Ext.form;

        var cm = new Ext.grid.ColumnModel([
        {
            header: "文件号", width: 150, dataIndex: 'DocNo1'
        }, {
            header: "文件名", width: 350, dataIndex: 'DocName'
        }, {
            header: "序号", width: 50, dataIndex: 'OrderNo'
        }, {
            header: "操作", width: 150, dataIndex: 'Id', renderer: function (value, a, reader, c, d, ds) {
                var links = [];
                links.push('<a href="#" onclick="var url =\'DocNoEdit.aspx?DocId=' + value + '\';window.showModalDialog(url, window, \'dialogWidth=850px;dialogHeight=550px;status=no;help=no;resizable=yes;\');Ext.getCmp(\'DocNoMgr\').getStore().reload();">编辑</a>');
                links.push('<a href="#" onclick="return delDocNo(\'' + value + '\');">删除</a>');

                return links.join("&nbsp;");
            }           
        }]);
        var reader = new Ext.data.JsonReader({
            totalProperty: 'totalCount', //分页使用
            root: 'data',
            autoLoad: true,
            fields: [{
                name: 'DocNo1'
            }, {
                name: 'DocName'
            }, {
                name: 'OrderNo'
            },{
                name: 'Id'
            }]
        });

        var ds = new Ext.data.GroupingStore({
            scopte: this,
            id: 'DocNoDS',
            url: '/DocNo.mvc/Find',
            reader: reader
        });
        Ext.apply(this, {
            id: 'DocNoMgr',
            plain: false,
            layout: 'fit',
            border: false,
            cm: cm,
            frame: true,
            store: ds,
            loadMask: true,
            clicksToEdit: 1,
            tbar: [{
                text: '添加', iconCls: 'icon-edit',
                handler: function () {
                    var url = 'DocNoEdit.aspx';
                    var retVal = window.showModalDialog(url, window, 'dialogWidth=850px;dialogHeight=550px;status=no;help=no;resizable=yes;');
                    if (retVal != undefined && retVal.indexOf('Form') >= 0) {
                        ds.reload();
                    }
                }
            },
            /*{
            text: "保存",
            tooltip: "保存修改后的信息",
            iconCls: "icon-save",
            handler: function () {
            var records = [];
            var grid = this.getStore();
            var isSubValid = true;
            grid.each(function (rs) {
            if (rs.get('isNew') == '1') {
            if (rs.get('DocNo1') == '' || rs.get('DocName') == '' || rs.get('OrderNo') == '') {
            isSubValid = false;
            return;
            }
            records.push({ DocNo1: rs.get('DocNo1'), DocName: rs.get('DocName'), OrderNo: rs.get('OrderNo') });
            } else if (rs.dirty) {
            if (rs.get('DocNo1') == '' || rs.get('DocName') == '' || rs.get('OrderNo') == '') {
            isSubValid = false;
            return;
            }
            records.push({ Id: rs.get('Id'), DocNo1: rs.get('DocNo1'), DocName: rs.get('DocName'), OrderNo: rs.get('OrderNo') });
            }
            });
            if (!isSubValid) {
            Ext.ux.Toast.msg('提示', '请输入文件编号/名称和排序号！');
            return;
            }
            this.jsonData = Ext.util.JSON.encode(records);
            Ext.Ajax.request(
            {
            url: '/DocNo.mvc/AddOrUpdate',
            success: function () {
            Ext.ux.Toast.msg('提示', '已经成功保存数据');
            ds.reload();
            },
            failure: function (action) {
            if (action.failureType == 'server') {
            obj = Ext.util.JSON.decode(action.response.responseText);
            Ext.Msg.alert('错误:', obj.errors.reason);
            }
            },
            params: {
            json: this.jsonData
            },
            waitMsg: '保存数据中...'
            });
            grid.commitChanges();
            } .createDelegate(this)
            },{
            text: '删除', iconCls: 'icon-delete', minWidth: '50',
            handler: function () {
            var selections = this.selModel.getSelections();
            var n = selections.length;
            if (n == 0) {
            Ext.Msg.alert("系统提示:", "请至少选择一项！");
            return
            }
            Ext.Msg.show(
            {
            title: '系统确认', msg: '您是否确定删除这些记录?删除的数据无法恢复！', buttons: Ext.Msg.YESNO, scope: this,
            fn: function (btn) {
            if (btn == 'yes') {
            store = this.getStore();
            var rdIds = [];
            for (var i = 0; i < n; i++) {
            rdIds.push(selections[i].data.Id);
            store.remove(selections[i])
            }
            var rdsjson = Ext.util.JSON.encode(rdIds);
            this.deleteSelectedRows(rdsjson);
            }
            },
            icon: Ext.MessageBox.QUESTION
            })
            }
            .createDelegate(this)
            },*/'-', {
                text: "重新加载数据",
                iconCls: 'icon-redo',
                handler: function () {
                    ds.reload();
                }
            }],

            //分页
            bbar: new Ext.PagingToolbar({
                pageSize: 15,
                store: ds,
                displayInfo: true,
                displayMsg: '显示第 {0} 条到 {1} 条记录，共 {2} 条',
                emptyMsg: '无记录',
                hidden: false
            }),
            loadMask: true
        });
        Swfu.BackMgr.DocNoMgr.superclass.initComponent.apply(this, arguments);
        ds.load({
            params: {
                start: 0, limit: 15
            }
        });
    },
    deleteSelectedRows: function (rdIds) {
        var ds = this.getStore();
        Ext.Ajax.request(
        {
            url: '/DocNo.mvc/Delete',
            success: function (response, options) {
                var responseMessage = Ext.util.JSON.decode(response.responseText);
                if (responseMessage.Result) {
                    ds.reload();
                    Ext.ux.Toast.msg('提示', '已经成功删除选中的数据。');
                }
                else {
                    Ext.ux.Toast.msg('提示', '删除失败，请您重试。');
                }
            },
            failure: function (action) {
                if (action.failureType == 'server') {
                    obj = Ext.util.JSON.decode(action.response.responseText);
                    Ext.Msg.alert('错误:', obj.errors.reason);
                }
            },
            params: {
                json: rdIds
            }
        })
    } /*,
    render: function () {
        Swfu.BackMgr.DocNoMgr.superclass.render.apply(this, arguments);
        //超级管理员2,熊正浩180
        if (",2,180,".indexOf("," + Cookies.get("empId") + ",") == -1) {
            this.getTopToolbar().items.item(0).disable();
        }
    }*/
});

Ext.reg('DocNoMgr', Swfu.BackMgr.DocNoMgr);